<template>
  <div class="resslist">
    <van-address-list
      v-model="chosenAddressId"
      :list="list"
      :disabled-list="disabledList"
      disabled-text="以下地址超出配送范围"
      default-tag-text="默认"
      @add="onAdd"
      @edit="onEdit"
      @select="onselect"
    />
  </div>
</template>

<script>
import router from "@/router";
import request from "@/utils/request";
import { showConfirmDialog } from "vant";
import { showNotify } from "vant";
export default {
  data() {
    return {
      chosenAddressId: "",
      list: [],
      disabledList: [
        {
          id: "3",
          name: "王五",
          tel: "1320000000",
          address: "浙江省杭州市滨江区江南大道 15 号",
        },
      ],
      isfinalorder: "",
    };
  },
  async created() {
    this.isfinalorder = this.$route.query.isfinalorder;
    this.getresslistinfo();
  },
  methods: {
    async getresslistinfo() {
      const result = await request.get("/good/getresslist");
      if (result.data.code === 200) {
        // 重构地址列表
        result.data.resslistinfo.forEach((item) => {
          this.list.push({
            id: item["id"],
            name: item["name"],
            tel: item.tel,
            address:
              item["province"] +
              item["city"] +
              item["county"] +
              item["addressDetail"],
            isDefault: item["isDefault"].length == 4 ? true : false,
          });
        });
      }
    },
    // 新增地址
    onAdd() {
      router.push("/addresslist");
    },
    // 编辑地址
    onEdit(item) {
      router.push({ path: "/addresslist", query: { id: item.id } });
    },
    // 切换地址
    onselect(item) {
      showConfirmDialog({
        message: "是否选择此地址为默认地址",
      })
        .then(async () => {
          const result = await request.get(
            `/good/updatedefaultresslist?id=${item.id}`
          );
          if (result.data.code === 200) {
            showNotify({ type: "success", message: result.data.message });
            if (this.isfinalorder) {
              router.replace("/finalorder");
            }
          }
        })
        .catch(() => {
          console.log("点击取消");
        });
    },
  },
};
</script>

<style lang="scss"></style>
